package prjCode;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.WritableComparable;


public abstract class TaggedKey<T extends WritableComparable,K extends WritableComparable> 
implements WritableComparable<TaggedKey<T,K>> {

	// An implementation of value with tag, as a writable object

	protected T tag;
	protected K key;

	TaggedKey() {
		init();
	}

	TaggedKey(T tag) {
		this.tag = tag;
		this.key = null;
	}

	TaggedKey(T tag,K key) {
		this.tag = tag;
		this.key = key;
	}

	protected abstract void init();

	@Override
	public void readFields(DataInput data) throws IOException {
		tag.readFields(data);
		key.readFields(data);
	}

	@Override
	public void write(DataOutput data) throws IOException {
		tag.write(data);
		key.write(data);
	}

	@Override
	public int compareTo(TaggedKey<T,K> other) {
		int i = key.compareTo(other.key);
		if (i==0)
			i=tag.compareTo(other.tag);
		return i;
	}

	@Override    
	public String toString() {
		return key + " " + tag;
	}

	@Override
	public boolean equals(Object o) {
		TaggedKey<T,K> other = (TaggedKey<T,K>)o;
		return tag.equals(other.tag) && key.equals(other.key);
	}

	public T getTag() { return tag; }
	public K getKey() { return key; }
	public void setKey(K key) { this.key = key; }
	public void setTag(T tag) { this.tag = tag; }

}

